package org.chen.org.service.impl;

import org.chen.bas.service.lmpl.BasServiceImpl;
import org.chen.org.mapper.DepartmentMapper;
import org.chen.org.service.IDepartmentService;
import org.chen.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BasServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper mapper;
    @Transactional
    @Override
    public void insert(Department department) {
        mapper.insert(department);
        if (Objects.nonNull(department.getParent().getId())){
            //根据这个父级的id 去查询父级的对象 然后父级对象的path 拼上自己的path
            Department parent =selectById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());
        }else {
            //父级id为空 就是一级部门
            department.setPath("/"+department.getId());
        }mapper.update(department);
    }
    @Transactional
    @Override
    public void update(Department department) {
        if (Objects.nonNull(department.getParent().getId())){
            //根据这个父级的id 去查询父级的对象 然后父级对象的path 拼上自己的path
            Department parent =selectById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());
        }else {
            //父级id为空 就是一级部门
            department.setPath("/"+department.getId());
        }
        mapper.update(department);
    }



    @Override
    public List<Department> getSuper() {
        return mapper.getSuper();
    }
}
